package my.georings;


import my.georings.R;
import my.georings.data.LocationManagerAdapter;
import my.georings.data.MyLocation;
import my.georings.GeoPhoneRingsApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;


public class MyListActivity extends Activity implements 
AdapterView.OnItemClickListener, 
DialogInterface.OnClickListener
{

	private LocationManagerAdapter adapter;
	private AlertDialog menuAlert;
	private MyLocation current;
	private final CharSequence[] items = {"Edit", "Show on map", "Remove"};
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        this.adapter = new LocationManagerAdapter(this, R.layout.row, ((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().getList());

		((ListView)findViewById(R.id.ListView)).setAdapter(adapter);
        ((ListView)findViewById(R.id.ListView)).setOnItemClickListener(this);
    }
	
	
	@Override
	protected void onResume(){
		super.onResume();
		adapter.sortByName();
		adapter.notifyDataSetChanged();
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
		
		int theId = Integer.valueOf(view.findViewById(R.id.ItemToggleButtonLocationOn).getContentDescription().toString());
		current = ((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().getLocationOfId(theId);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(current.getName());
		
		builder.setItems(items, this);
		menuAlert = builder.create();
		menuAlert.show();
	}
	
	
	@Override
	public void onClick(DialogInterface dialog, int which) {
		//menuAlert.dismiss();
		switch(which){
		case 0:
			Intent myIntent = new Intent(this, my.georings.dialog.EditDialog.class);
			myIntent.putExtra("theId", current.getId());
			startActivityForResult(myIntent, R.id.TextViewLocationCurrent);
			break;
			
		case 1:
			Intent myNextIntent = new Intent(this, my.georings.dialog.ShowOnMapDialog.class);
			myNextIntent.putExtra("theId", current.getId());
			startActivity(myNextIntent);
			break;
			
		case 2:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Removing location");
			builder.setMessage("Are you sure you want to remove " + current.getName());
			builder.setCancelable(false);
			builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().removeLocation(current.getId());
					adapter.notifyDataSetChanged();
					menuAlert.dismiss();
					dialog.dismiss();
				}
			});
			builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			
			AlertDialog confirm = builder.create();
			confirm.show();
			break;
		}
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
		if(requestCode == R.id.TextViewLocationCurrent && resultCode == RESULT_OK){
			adapter.notifyDataSetChanged();
		}
	}
	

}
